<?php

class Dao_ProductClassDAO extends Dao_AbstractDAO {
	
	public function __construct($db = null) {
		parent::__construct($db);
		$this->tableName = "productclass";
		$this->key = "id";
		$this->classModel = "Model_ProductClass";
	}
	
	public function getProductClass($page = null, $number = null) {
		$query = $this->createSQLGetProductClass();
		$query = $this->appendLimit($query, $page, $number);
		return $this->getList($query);
	}
	
	private function createSQLGetProductClass() {
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE T.delete_flg = 0 ";
		$query .= " ORDER BY T.order_index ASC ";
		return $query;
	}
	/**
	 * 
	 * @param string $id
	 * @return Model_ProductClass
	 */
	public function getProductClassByCategoryId($id) {
		$query = $this->createSQLGetProductClassByCategoryId($id);
		return $this->getList($query);
		//return $this->getOneObject($query);
	}
	
	private function createSQLGetProductClassByCategoryId($id) {
		$id = $this->removeEscape($id);
		$query = " SELECT T1.* FROM $this->tableName T1 ";
		$query .= " left join category T2 on T1.id_category = T2.id ";
		if ($id !== null) {
			$query .= " WHERE T1.id_category = '" . $id . "'" ;
		}
		return $query;
	}
	
	
	public function getProductClassWithoutDelete($start = null, $number = null) {
		$query = $this->createSQLGetProductClassWithoutDelete($start, $number);
		$query = $this->appendLimit($query, $start, $number);
		return $this->getList($query);
	}
	
	public function countProductClassWithoutDelete() {
		$query = $this->createSQLCountProductClassWithoutDelete();
		$res = $this->query($query);
		if (count($res) > 0) {
			return $res[0]['CNT'];
		} else {
			return 0;
		}
	}
	
	private function createSQLGetProductClassWithoutDelete($start = null, $number = null) {
		$query = " SELECT T.*, cate.name as category_name FROM $this->tableName T ";
		$query .= " LEFT OUTER JOIN category cate ";
		$query .= " ON cate.id = T.id_category ";
		return $query;
	}
	
	private function createSQLCountProductClassWithoutDelete() {
		$query = " SELECT COUNT(*) AS CNT FROM $this->tableName T ";
		return $query;
	}
	
	/**
	 * 
	 * @param string $id
	 * @return Model_ProductClass
	 */
	public function getProductClassById($id) {
		return $this->getModelById($id);
	}
	
	public function insertProductClass(Model_ProductClass $proclss){
		if ($proclss === null) {
			return false;
		}
	
		$this->startTransaction();
		$success = $this->insertModel($proclss);
	
		if ($success) {
			$this->commitTransaction();
			return true;
		} else {
			$this->rollBackTransaction();
			return false;
		}
	
		return $success;
	}
	
	public function updateProductClass(Model_ProductClass $proclss){
		if ($proclss === null) {
			return false;
		}
		$success = false;
	
		$sql = $this->startTransaction();
	
		$success = $this->updateModelById($proclss);
	
		if ($success) {
			$this->commitTransaction();
		} else {
			$this->rollBackTransaction();
		}
	
		return $success;
	}
	
	public function insertOrUpdate(Model_ProductClass $proclss) {
		$success = false;
		if ($proclss->getId() !== null) {
				
			$success = $this->updateProductClass($proclss);
		} else {
			$success = $this->insertProductClass($proclss);
		}
		return $success;
	}
	
	
}